
module.exports = app => {
  const DataTypes = app.Sequelize;

  const ProductsClasses = app.model.define(
    "products_classes",
    {
      id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      pid: {
        type: DataTypes.INTEGER,
        comment: "商品id",
      },
      cid: {
        type: DataTypes.INTEGER,
        comment: "分类表id",
      },
      createdAt: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: "创建时间",
      },
      updatedAt: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: "更新时间",
      },
    },
    {
      tableName: "products_classes",
    }
  );

  ProductsClasses.associate = function () {
    app.model.Products.belongsToMany(app.model.Classes, {
      through: app.model.ProductsClasses,
      foreignKey: "pid",
      otherKey: "cid",
    });

    app.model.Classes.belongsToMany(app.model.Products, {
      through: app.model.ProductsClasses,
      foreignKey: "cid",
      otherKey: "pid",
      separate: true
    });
  };

  return ProductsClasses;
};
